package com.cinyi.cyds.test;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.cinyi.ormlite.ConfigurationException;
import com.cinyi.ormlite.IContext;
import com.cinyi.ormlite.web.WebContext;
import com.cinyi.ormlite.web.dcp.DbConnectionManager;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring.xml", 
        "classpath:spring-ehcache.xml",
        "classpath:spring-main.xml",
        "classpath:spring-druid.xml",
        "classpath:spring-mvc.xml",
        "classpath:spring-tasks.xml" })
public abstract class BaseCase
{
    protected Log logger = LogFactory.getLog(BaseCase.class);

    @Autowired
    ApplicationContext ctx;

    @Before
    public void setup() {
    	logger.info("-------------- cinyi setup");
		IContext context = new WebContext(ctx) ;
		try {
			context.addBundle("config");
			context.addBundle("message");
		} catch (ConfigurationException e) {
			e.printStackTrace();
		}
		
		
		logger.info("setup,cfg session  info:"+context.getConfiguration().get("session_info"));
	
		DbConnectionManager.getInstance().start(context.getConfiguration());

    }
    
    @After
    public void destory(){
//        MongoManager.getInstance().destroy();
//        RedisManager.getInstance().destroy();
    	
    	DbConnectionManager.getInstance().destroy();
    	
    	logger.info("-------------- case destory");
    }
    
    
    public Object getBean(String name) {
        return ctx.getBean(name);
    }

    protected ApplicationContext getContext() {
        return ctx;
    }

    public <T> T getBean(Class<T> type) {
        return ctx.getBean(type);
    }
    
    public void log(String msg){
        logger.error(msg);
    }

    public void e(String msg){
        System.err.println(msg);
    }
    
}